﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Linq.Expressions;
using Ice.Toolkit.Data.Entity;
using Ice.Toolkit.Data.Sql;

namespace Ice.Toolkit.Data
{
    internal enum OrderByType
    { 
        ASC = 1,
        DESC = 2
    }
    /// <summary>
    /// SQL查询对像
    /// </summary>
    /// <typeparam name="T"></typeparam>
    public class Query<T> : IQuery<T> where T : EntityBase<T>, new()
    {
        private List<Expression<Func<T, bool>>> _whereList;
        private List<String> _orderByList;
        private int _count;
        private DataContext _dataContext;

        public Query(DataContext dataContext)
        {
            _whereList = new List<Expression<Func<T, bool>>>();
            _orderByList = new List<String>();
            _count = -1;
            _dataContext = dataContext;
        }
        /// <summary>
        /// Where条件
        /// </summary>
        /// <param name="expression">条件表达式</param>
        /// <returns></returns>
        public IQuery<T> Where(Expression<Func<T, bool>> expression)
        {
            _whereList.Add(expression);

            return this;
        }
        /// <summary>
        /// 获取前多少行
        /// </summary>
        /// <param name="count">数量</param>
        /// <returns></returns>
        public IQuery<T> Take(int count)
        {
            _count = count;
            return this;
        }
        /// <summary>
        /// 升序
        /// </summary>
        /// <typeparam name="TKey"></typeparam>
        /// <param name="expression"></param>
        /// <returns></returns>
        public IQuery<T> OrderBy<TKey>(Expression<Func<T, TKey>> expression)
        {
            _orderByList.Add(new SqlOrderBy<T, TKey>(expression, OrderByType.ASC).SqlOrderByText);
            return this;
        }
        /// <summary>
        /// 降序
        /// </summary>
        /// <typeparam name="TKey"></typeparam>
        /// <param name="expression"></param>
        /// <returns></returns>
        public IQuery<T> OrderByDescending<TKey>(Expression<Func<T, TKey>> expression)
        {
            _orderByList.Add(new SqlOrderBy<T, TKey>(expression, OrderByType.DESC).SqlOrderByText);
            return this;
        }
        /// <summary>
        /// 行数
        /// </summary>
        /// <returns></returns>
        public Int32 Count()
        {
            SelectBuilder<T> builder = new SelectBuilder<T>(_dataContext, _whereList, _orderByList, _count, true);

            builder.Build();

            return _dataContext.ExecuteScalar<Int32>(builder.SQLText, builder.DbParameters);
        }
        /// <summary>
        /// 获取唯一数据
        /// </summary>
        /// <returns></returns>
        public T Single()
        {
            return ToList().FirstOrDefault();
        }
        /// <summary>
        /// 获取多行数据
        /// </summary>
        /// <returns></returns>
        public List<T> ToList()
        {
            SelectBuilder<T> builder = new SelectBuilder<T>(_dataContext, _whereList, _orderByList, _count);

            builder.Build();
            
            return _dataContext.Query<T>(builder.SQLText, builder.DbParameters);
        }
    }
}
